package shortlink.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import shortlink.common.biz.user.UserContext;
import shortlink.common.convention.exception.ServiceException;
import shortlink.common.convention.result.Result;
import shortlink.dao.entity.GroupDO;
import shortlink.dao.mapper.GroupMapper;
import shortlink.remote.ShortLinkRemoteService;
import shortlink.remote.dto.req.ShortLinkRecycleBinPageReqDTO;
import shortlink.remote.dto.resp.ShortLinkPageRespDTO;
import shortlink.service.RecycleBinService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@RequiredArgsConstructor
@Service
public class RecycleBinServiceImpl implements RecycleBinService {
    private  final GroupMapper groupMapper;
    private  final ShortLinkRemoteService shortLinkRemoteService;
    @Override
    public Result<IPage<ShortLinkPageRespDTO>> pageRecycleBinShortLink(ShortLinkRecycleBinPageReqDTO requestParam) {
        LambdaQueryWrapper<GroupDO> queryWrapper = Wrappers.lambdaQuery(GroupDO.class)
                .eq(GroupDO::getUsername, UserContext.getUsername())
                .eq(GroupDO::getDelFlag, 0);
        List<GroupDO> groupList=groupMapper.selectList(queryWrapper);
        if(CollUtil.isEmpty(groupList)){
            throw  new ServiceException("用户无分组信息");
        }
        requestParam.setGidList(groupList.stream().map(GroupDO::getGid).toList());
        return shortLinkRemoteService.pageRecycleBinShortLink(requestParam.getGidList(),requestParam.getCurrent(), requestParam.getSize());
    }


}
